package com.ktjiaoyu.server.config.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;
import javax.annotation.processing.Filer;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Resource
    JwtAuthencationTokenFilter jwtAuthencationTokenFilter ;
    @Resource
    RestAuthorizationEntryPoint restAuthorizationEntryPoint;
    @Resource
    RestfulAccessDeniedHandler restfulAccessDeniedHandler;
    @Resource
    CustomFilter customFilter;
    @Resource
    CustomUrlDecisionManager customUrlDecisionManager;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //授权配置
        http.authorizeRequests()
                .anyRequest().authenticated()
                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {

                    @Override
                    public <O extends FilterSecurityInterceptor> O postProcess(O o) {
                        o.setSecurityMetadataSource(customFilter);
                        o.setAccessDecisionManager(customUrlDecisionManager);
                        return o;
                    }
                });

        //添加拦截器
        http.addFilterBefore(jwtAuthencationTokenFilter
                ,UsernamePasswordAuthenticationFilter.class);

        //不要跨域验证
        http.csrf().disable();

        //异常处理
        http.exceptionHandling()
                .authenticationEntryPoint(restAuthorizationEntryPoint)//未认证异常处理
                .accessDeniedHandler(restfulAccessDeniedHandler);//没有权限

        //禁用session
        http.sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers(
                        "/login",
                        "/logout",
                        "/captcha",
                        "/css/**",
                        "/js/**",
                        "/index.html",
                        "favicon.ico",
                        "/swagger-ui.html",
                        "/doc.html",
                        "/webjars/**",
                        "/swagger-resources/**",
                        "/v2/api-docs/**"
                );
    }
}
